package cn.lg.soar.system.auth.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.system.account.service.ISsoServerService;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.auth.entity.SsoApply;
import cn.lg.soar.system.auth.service.ISsoApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@ConditionalOnProperty(name = "lg.sso.server.enabled", havingValue = "true")
@Api(tags = "单点登录应用")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/sso-apply")
public class SsoApplyController extends BaseController<SsoApply, ISsoApplyService> {

    @Autowired
    private ISsoServerService ssoServerService;

    @Override
    protected Serializable getId(SsoApply entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "应用";

    @Log(value = "新增" + SUBJECT, data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Serializable> create(@RequestBody @Validated SsoApply data) {
        return createResult(data);
    }

    @Log(value = "修改" + SUBJECT, data = "#p0", dataId = "#p0.id")
    @UpdateMapping
    public HttpResult<Void> update(@RequestBody @Validated SsoApply data) {
        return updateResult(data);
    }

    @Log(value = "删除" + SUBJECT, data = "#p0", type = Log.Type.TEXT)
    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @DetailMapping
    public HttpResult<SsoApply> detail(@Id Long id) {
        return detailResult(id);
    }

    @QueryMapping
    public HttpResult<PageResult<SsoApply>> query(PageQuery pageQuery, QueryParams params){
        return super.queryResult(pageQuery, params);
    }

    @ListMapping
    public HttpResult<List<SsoApply>> list(QueryParams params){
        return HttpResult.data(
                super.getList(params)
        );
    }

    @Log(value = "导出" + SUBJECT, data = "#p0")
    @ExportMapping
    public void exports(String filename, QueryParams params, HttpServletResponse resp){
        super.exportExcel(filename, params, resp);
    }

    @Log(value = "导入" + SUBJECT)
    @ImportMapping
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @ApiOperation("清空缓存")
    @PostMapping(value = "clearCache", name = "清空缓存")
    public HttpResult<Void> clearCache(){
        this.service.clearCache();
        return HttpResult.success();
    }

    @ApiOperation("获取 ticket")
    @GetMapping(value = "ticket", name = "获取 ticket")
    public HttpResult<String> ticket(){
        return HttpResult.data(
                this.ssoServerService.getTicket(UserContext.getUserId())
        );
    }
}
